Skip to content

Conversation

@AlexanderSinn
Copy link
Member

Summary

This PR adds a SetArena(Arena*) function to ParticleContainerBase that allows setting a memory arena that is used for all the particle vectors if the allocator is PolymorphicArenaAllocator. The function has to be called before particle tiles are defined.

This functionality is used to fix a bunch of places where a polymorphic vector would previously not have its arena set properly.

Additionally the RunOnGpu logic in AMReX_WriteBinaryParticleData.H is extended to work with a polymorphic allocator.

Uses changes extracted from #4404.

Additional background

Previously all components of all particle tiles needed their arena set individually by the user if PolymorphicArenaAllocator was used. In case this was done this PR is a braking change due to the AMREX_ALWAYS_ASSERT_WITH_MESSAGE(a_arena != nullptr assert in ParticleTile::define().

Checklist

The proposed changes:

  • fix a bug or incorrect behavior in AMReX
  • add new capabilities to AMReX
  • changes answers in the test suite to more than roundoff level
  • are likely to significantly affect the results of downstream AMReX users
  • include documentation in the code and/or rst files, if appropriate

@atmyers atmyers merged commit 7f21584 into AMReX-Codes:development Aug 13, 2025
75 checks passed
Comment on lines +252 to +254
void SetArena (Arena* a) {
m_arena = a;
}
Copy link
Member

@ax3l ax3l Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexanderSinn @atmyers I am wondering: should we add an assert here that setting the arena can only be done once?

There are already good asserts / exceptions now if a PC with an unset allocator is trying to add particles. Thus, the logic here could be as easy as: assert if m_arena is not nullptr when callint SetArena. What do you think?

I want to avoid that someone calls SetArena for an already defined PC with live particles and gets into an unexpected state by that.

Suggested change
void SetArena (Arena* a) {
m_arena = a;
}
void SetArena (Arena* a) {
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_arena == nullptr, "SetArena can only be called once per PC!");
m_arena = a;
}

(Technically, we can make it more complicated, like allowing to switch the arena until any tiles/particles are added. That could be useful in some usage patterns when specializing a user container that might set a default in its constructor.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is the a_arena == GetStructOfArrays().GetIdCPUData().arena() check in ParticleTile that should catch this issue, but I guess we could add another check to ParticleContainerBase 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants